



Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: undefined,
    password: undefined,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // @ts-ignore
  async onLoad(options: any) {
    await this.login()
  },



  phoneChange(event: any) {
    let phone = event.detail as string;
    console.log(phone);
    this.setData({
      phone: phone
    })
    return phone
  },

  passwordChange(event: any) {
    let password = event.detail as string;
    console.log(password);
    this.setData({
      password: password
    })
    return password
  },



  //自动登录，从本地取出相关信息，然后进行登录。如果是首次登录，那么请求个人信息，如果已经登录过，那么检测是否是isAudit状态，如果是，那么直接进入，如果不是，提示登录失败
  async login() {
    try {
      wx.showLoading({ title: '登录中' });
      let name = wx.getStorageSync('name');
      let phone = wx.getStorageSync('phone');
      let password = wx.getStorageSync('password');
      let inputPhone = this.data.phone;
      let inputPassword = this.data.password;
      console.log(name, phone, password);

      //判断是否填写了参数
      if (inputPhone && inputPassword) {
        console.log('weiye');
        let res = await wx.cloud.database().collection('users').where({ phone: inputPhone, password: inputPassword, isOpen: true }).get();
        console.log(res);
        if (res.data.length > 0) {
          let user = res.data[0];
          wx.setStorageSync('phone', user.phone)
          wx.setStorageSync('name', user.name)
          wx.setStorageSync('password', user.password);
          wx.setStorageSync('role', user.role);
          let app = getApp();
          app.globalData.userName= user.name
          app.globalData.userPhone= user.phone
          app.globalData.userPassword= user.password
           app.globalData.userRole= user.userRole
          wx.showToast({
            title: '登录成功',
            icon: "none"
          });
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else {
          wx.showToast({
            title: '登录失败,请填写正确的账号密码',
            icon: "none"
          })
        }

      } else {

        //本地有参数,那么尝试进行登录
        // @ts-ignore
        wx.cloud.database().collection('users').where({ phone: phone, password: password, isOpen: true }).get().then(async res => {
          if (res.data.length > 0) {
            let user = res.data[0]
            wx.showToast({
              title: '登录成功',
            });
            let phone = user.phone;
            let password = user.password;
            let name = user.name;
            let role = user.role;
            let app = getApp();
            app.globalData.userName= user.name
            app.globalData.userPhone= user.phone
            app.globalData.userPassword= user.password
             app.globalData.userRole= user.userRole
            wx.setStorageSync('phone', phone)
            wx.setStorageSync('password', password)
            wx.setStorageSync('name', name)
            wx.setStorageSync('role', role);
            wx.switchTab({
              url: '/pages/index/index',
            })
          } else {
            wx.showToast({
              title: '登录失败,联系管理员开通权限',
              icon: "none"
            })
          }})

      }

    } catch (error) {
      console.log(error);
      wx.showToast({
        title: '登录失败',
        icon: "none"
      })

    } finally {
      wx.hideLoading()
    }
  },






  async getUserInfo() {
    try {
      let res = await wx.getUserProfile({ desc: '用于完善人员资料', });
      let userInfo = res.userInfo;
      console.log(userInfo);
      if (userInfo) {
        this.setData({
          userInfo: userInfo,
          isAuthUserInfo: true
        });
        await this.login()
        return true
      } else {
        wx.showToast({ title: "请授权个人信息", icon: 'none' });
        return false
      }
    } catch (error) {
      wx.showToast({ title: "请授权个人信息", icon: 'none' });
      return false
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})